/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.details;

import com.vaadin.addon.touchkit.ui.HorizontalComponentGroup;

import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.themes.BaseTheme;
import org.processbase.touchprocess.ui.TouchProcessApplication;

public class DetailsView extends NavigationView implements ClickListener {

    private static final long serialVersionUID = 1L;
    private CssLayout layout = new CssLayout();
    private Component detailsLayout = null;
    private Button btnClose;
    private Button btnAbout;
    private AboutPopover aboutPopover = null;
    private Button btnLogo;
    private Resource iconLogo = new ThemeResource("images/logo.png");
    private Resource iconClose = new ThemeResource("images/power_blue.png");
    private Resource iconRefresh = new ThemeResource("images/sub_blue_rotate_ccw.png");
    private HorizontalComponentGroup toolbarRight = null;
    private HorizontalComponentGroup toolbarLeft = null;
    private boolean smartphone;

    public DetailsView(boolean smartphone) {
        this.smartphone = smartphone;
    }

    public void initUI() {
        setContent(layout);
        layout.setWidth("100%");
        layout.setStyleName("message-layout");
        addStyleName("message-view");

        buildToolbar();
        setCaption(TouchProcessApplication.getCurrent().getUserName());

        if (smartphone) {
            setToolbar(toolbarRight);
//            setRightComponent(navigationActions);
        } else {
//            toolbar.setStyleName(null);
//            toolbar.setWidth("150px");
//            toolbar.setHeight("32px");
            setRightComponent(toolbarRight);
            setLeftComponent(toolbarLeft);
        }

        setEmptyDetailsLayout();

    }

    public void setEmptyDetailsLayout() {
        if (detailsLayout != null) {
            layout.removeComponent(detailsLayout);
        }
        addStyleName("no-message");
        detailsLayout = new Button(null, this);
        detailsLayout.setIcon(iconLogo);
        detailsLayout.setStyleName(BaseTheme.BUTTON_LINK);
        ((Button) detailsLayout).addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                getWindow().open(new ExternalResource("http://www.processbase.biz"));
            }
        });

        layout.addComponent(detailsLayout);
    }

    public void setDetailsLayout(Component component) {
        if (detailsLayout != null) {
            layout.removeComponent(detailsLayout);
        }
        detailsLayout = component;
        removeStyleName("no-message");
        layout.addComponent(component);
        layout.setWidth("100%");
    }

    private void buildToolbar() {
        toolbarLeft = new HorizontalComponentGroup();
        btnAbout = new Button("About", this);
//        btnAbout.setStyleName("no-decoration");
//        btnAbout.setIcon(iconRefresh);
        toolbarLeft.addComponent(btnAbout);

        aboutPopover = new AboutPopover();
        aboutPopover.initUI();

        toolbarRight = new HorizontalComponentGroup();
        btnClose = new Button("Exit", this);
//        btnClose.setStyleName("no-decoration");
//        btnClose.setIcon(iconClose);
        toolbarRight.addComponent(btnClose);
    }

    public void buttonClick(ClickEvent event) {
        if (event.getButton().equals(btnClose)) {
            TouchProcessApplication.getCurrent().close();
        } else if (event.getButton().equals(btnAbout)) {
            aboutPopover.showRelativeTo(btnAbout);
            return;
        }
    }

    public void showImportantInformation(String description) {
        StringBuilder desc = new StringBuilder();
        if (description != null) {
            int i = 0;
            for (; description.length() > i + 50; i = i + 50) {
                desc.append("<br/> ").append(description.substring(i, i + 50));
            }
            desc.append("<br/> ").append(description.substring(i));
        } else {
            desc.append("<br/> null");
        }
        getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("informationCaption"), desc.substring(0), Notification.TYPE_ERROR_MESSAGE);

    }
}
